<template>
<div style="width: 100%">
  <a-card class="card" :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" :lg="{span:12}" :xl="{span:6}">
            <a-form-item label="招生年度">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zsxn">
                <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:24}" style="text-align:center">
            <a-button type="primary" @click="handleReport" :disabled="!editable">上报</a-button>
            <a-button type="primary" @click="handleCreate" :disabled="!editable">添加</a-button>
            <a-button type="primary" @click="loadData">查询</a-button>
            <a-button type="primary" :disabled="editable" @click="exportPDF(editable)">导出PDF</a-button>
            <a-button type="primary" @click="handleBatchDelete" :disabled="!editable">批量删除</a-button>
            <a-button type="primary" @click="previewExport" :disabled="!editable">预览导出</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    </a-card>
  <a-alert class="card"  message="※请维护完本院系所有信息后再上报、一旦上报禁止再进行任何修改" banner />
  <a-card class="card" :bordered="false">
    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :alert="options.alert"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      :pagination="pagination"
			@change="handleTableChange"
      :scroll="{ x: 1500 }"
    >
      <template slot="seq" slot-scope="text,record">
          <a-input placeholder="请输入..." :defaultValue="text"
          @blur="e => seqChange(record.id,'seq',e)" />
      </template>
      <template slot="action" slot-scope="text,record">
        <a @click="record.sbzt==0 && handleEdit(record.id)" :style="record.sbzt==0?'':'color:#999'">修改</a>
        <a-divider type="vertical" />
        <a @click="record.sbzt==0 && handleDelete(record.id)" :style="record.sbzt==0?'':'color:#999'">删除</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table>
    <!-- 新增 -->
    <college-recruit-add ref="collegeAdd" @close="handleCreateClose" @success="handleCreateSuccess" :addVisiable="addVisiable"></college-recruit-add>
    <!-- 编辑 -->
    <college-recruit-edit ref="collegeEdit" @close="handleEditClose" @success="handleEditSuccess" :info="editData"  :editVisible="editVisible"></college-recruit-edit>
  </a-card>
</div>
</template>

<script>
  // import moment from 'moment'
  import CollegeRecruitAdd from './collegeRecruitAdd'
  import CollegeRecruitEdit from './collegeRecruitEdit'
  import {STable} from '~'
  import {mapState} from 'vuex'

  const selectArr = ['TYPE0387']

export default {
  components: {
    STable,
    CollegeRecruitEdit,
    CollegeRecruitAdd
  },
  data () {
    return {
      advanced: true,
      loading: false,
      data: [],
      editable:true,
      // 查询参数
      queryParam: {
       zsxn:'',
       ssyxm:''
      },
      doctoralAdmissionYear:'',
      selectedRowKeys: [],
      selectedRows:'',
      postParams: {},
      pagination: {
					current: 1
        },
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' },
          width:80
        },
        {
          title: '院系',
          dataIndex: 'dwmc',
          width:100
        },
        {
          title: '专业代码',
          dataIndex: 'zyh',
          width:150
        },
        {
          title: '专业',
          dataIndex: 'zymc',
          width:150
        },
        {
          title: '研究方向码',
          dataIndex: 'yjfxm',
          width:150
        },
        {
          title: '研究方向',
          dataIndex: 'yjfxmc',
          width:150
        },
        {
          title: '导师',
          dataIndex: 'xm',
          width:100
        },
        {
          title: '招生人数',
          dataIndex: 'zsrs'
        },
        {
          title: '考试科目',
          dataIndex: 'kskm'
        },
        {
          title: '备注',
          dataIndex: 'note'
        },
        // {
        //   title: '排序',
        //   dataIndex: 'seq',
        //   scopedSlots: { customRender: 'seq' }
        // },
        {
          title: '操作',
          dataIndex: 'cz',
          fixed: 'right',
          width: 120,
          scopedSlots: { customRender: 'action' }
        }
      ],

      // custom table alert & rowSelection
      options: {
        alert: { show: false }
      },
      addVisiable: false,
      editVisible: false,
      selectMenu: {
        yx: [] // 院系
      },
      editData: {}
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0387: state => state.app['TYPE0387'] // 年级
    })
  },
  created () {
    this.initSelectMenu()

    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm=res[0].data[0].dwh;
        this.$api.base.getSchoolYear().then(res=>{
        this.queryParam.zsxn=res.data.doctoralAdmissionYear;
        // this.doctoralAdmissionYear=res.data.doctoralAdmissionYear;
        this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      })
      })


    },
    // 选择院系
    yxChange (code) {
    },
    // 加载数据
     loadData(parameter = {}) {
       if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.doctorRecruit.CollegeStudentList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
          this.data.forEach(item => {
            item.yjfxm = item.yjfxm.slice(-2)
          })
          let params={ssyxm:this.queryParam.ssyxm,zsxn:this.queryParam.zsxn}
          this.$api.doctorRecruit.CollegeStudentAble(params).then(res =>{
            this.editable=res.data;
          })
        }
        this.loading = false;
      })
    },
    // 分页
			handleTableChange(pagination, filters, sorter) {
				console.log(pagination);
				const pager = { ...this.pagination
				};
				pager.current = pagination.current;
				this.pagination = pager;
				this.loadData({
					pageSize: 10,
					pageNum: pagination.current
				});
			},
    // 选择招生年度
    zsndChange () {

    },
    //修改排序
    seqChange(key,dataIndex,e) {
     this.$api.doctorRecruit.CollegeStudentSort({id:key,seq:e.target.value}).then(res=>{
       this.data = []
       this.loadData({
         pageSize:10,
         pageNum:this.pagination.current
       })
      //  e.target.value = this.data[0].seq
     })
    },
    // 添加
    handleCreate () {
      this.addVisiable = true
    },
    // 关闭添加弹层
    handleCreateClose () {
      this.addVisiable = false
    },
    // 添加成功
    handleCreateSuccess () {
      this.addVisiable = false
      this.$message.success('添加成功')
       this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      })
    },
    // 修改
    handleEdit (key) {
      this.$api.doctorRecruit.CollegeStudentId(key).then(res=>{
        this.editData=res.data;
      })
      this.editVisible=true;
    },
    // 关闭添加弹层
    handleEditClose () {
      this.editVisible = false
    },
    // 添加成功
    handleEditSuccess () {
      this.editVisible = false
      this.$message.success('修改成功')
       this.loadData({
        pageSize: 10,
        pageNum: this.pagination.current
      })
    },
    // 导出
    exportExcel () {
      console.log('导出')
    },
    // 删除
    handleDelete (key) {
      let _this=this;
      this.$confirm({
         title: '确定删除?',
         onOk() {
          _this.$api.doctorRecruit.CollegeStudentDelete(key).then(res=>{
        if (res.status==1) {
          _this.loadData({
					pageSize: 10,
					pageNum: _this.pagination.current
        });
        _this.$message.success(res.msg)
        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
      onSelectChange (selectedRowKeys,selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows=selectedRows;
    },
    // 批量删除
    handleBatchDelete () {
      let _this=this;
         if (!_this.selectedRowKeys.length) {
        _this.$message.warning('请选择需要删除的信息')
        return
      }
      this.$confirm({
         title: '确定删除?',
          content:'被选择的所有未上报的记录将删除，已上报的记录不会被删除',
         onOk() {

      let para=[],params
      _this.selectedRows.forEach(item=>{
        para.push(item.id);
      })
      params=para.join(',')
      _this.$api.doctorRecruit.CollegeStudentDeleteForYx(params).then(res=>{
        console.log(res.status)
        if (res.status==1) {
           _this.selectedRowKeys = [];
          _this.loadData({
					pageSize: 10,
					pageNum: _this.pagination.current
        });
        _this.$message.success(res.msg)

        }else{
          _this.$message.warning(res.msg)
        }
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    // 导出PDF
    exportPDF (editable) {
      console.log('导出PDF',editable)
        if(!editable && this.queryParam.ssyxm){
        this.$export('admissions/zsBsszszyml/exportPdf',this.queryParam)
        }else{
          this.$message.warning('请选择院系')
        }
    },
    // 上报
    handleReport () {
      let _this=this;
      this.$confirm({
         title: '确定上报?',
         onOk() {
            let params={ssyxm:_this.queryParam.ssyxm,zsxn:_this.queryParam.zsxn}
        _this.$api.doctorRecruit.CollegeStudentUp(params).then(res =>{
        _this.loadData({
          pageSize:10,
          pageNum:_this.pagination.current
        })
      })
         },
         onCancel() {
           console.log('Cancel');
         },
         class: 'test',
       });

    },
    previewExport(){
      this.$export('admissions/zsBsszszyml/yldc',this.queryParam)
    }
  }
}
</script>

<style lang="less" scoped>
.card{
  margin-bottom: 20px;
}
</style>
